package com.martin.lib_base.net.interceptor

import com.martin.lib_base.net.body.ProgressResponseBody
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @author：Houge
 * @date：2022/4/10 20:10
 * @desc：进度拦截器
 */
class ProgressInterceptor(
    private val progressCallback: (url: String, total: Long, current: Long, progress: Int) -> Unit
) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val proceed = chain.proceed(chain.request())
        return proceed
            .newBuilder()
            .body(
                ProgressResponseBody(
                    chain.request().url.toString(),
                    proceed.body,
                    progressCallback
                )
            )
            .build()
    }
}